<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-card>
      <el-form :inline="true" :model="listQuery" size="small">
        <el-form-item>
          <el-input
            placeholder="投票题目"
            clearable
            v-model="listQuery.name"
            @keyup.enter.native="handleFilter"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-search"
            @click.native="handleFilter"
            >查询</el-button
          >
        </el-form-item>
        <!-- <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >
            新增{{ title }}
          </el-button>
        </el-form-item> -->
        <el-form-item>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="handleBatchDelete"
          >
            批量删除
          </el-button>
        </el-form-item>
      </el-form>

      <!-- table列表 -->
      <el-table
        style="width: 100%"
        border
        stripe
        size="small"
        :data="datas"
        v-loading="listLoading"
        highlight-current-row
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="#" align="center" type="selection" />
        <el-table-column
          v-for="column in tableColumns"
          :key="column.key"
          :label="column.title"
          :prop="column.key"
          :align="column.align ? column.align : 'center'"
          :width="column.width ? column.width : ''"
        >
        </el-table-column>
        <el-table-column align="center" label="操作" width="100px">
          <template slot-scope="scope">
            
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDelete(scope)"
              ></el-button
            >
            
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getDetailList"
      />
    </el-card>
    
  </div>
</template>

<script>

import Pagination from "@/components/Pagination";

import {
  getVoteDetails,
  delVoteDetail,
  delVoteDetails
} from "@/api/polls";


export default {
  name: "VoteDetail",

  data() {
    return {
 
      title: "投票详情",
      dialogType: "new",
      dialogVisible: false,

      datas: [],
      selections: [],
      
      listLoading: true,
      listQuery: {
        name: null,
        page: 1,
        page_size: 15,
      },
      total: 0,

      tableColumns: [
        {
          key: "openid",
          title: "微信ID",
          align: "left",
          width: "160px"
        },
        {
          key: "nickname",
          title: "昵称",
          align: "left",
          width: "120px"
        },
        
        {
          key: "question_name",
          title: "投票题目",
          align: "left",
        },
        {
          key: "choice_name",
          title: "选项",
          align: "left",
        },
        {
          key: "update_time",
          title: "时间",
          width: "140",
        }
      ],
      
    };
  },
  components: {
    Pagination,
  },
  computed: {
    getTitle() {
      return this.dialogType == "new"
        ? `新增${this.title}`
        : `修改${this.title}`;
    },

  },
  created() {

    this.getDetailList();

  },
  methods: {
    //
    getDetailList() {

      this.listLoading = true;

      getVoteDetails(this.listQuery)
        .then((res) => {

          this.datas = res.results;
          this.total = res.count;
          this.listLoading = false;

        })
        .catch((err) => {
          this.listLoading = false;
          this.$message.error("获取数据失败!");
        });
    },

    // 查询
    handleFilter() {

      this.listQuery.page = 1;

      this.getDetailList();

    },


    //删除数据
    handleDelete({ $index, row }) {
      this.$confirm("确定要删除吗?", "删除提示：", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await delVoteDetail(row.id);

          this.getDetailList();

          this.$message({
            type: "success",
            message: "删除成功!",
          });

        })
        .catch((err) => {

          console.error(err);
          this.$message({
            type: "error",
            message: "删除失败!",
          });

        });
    },

    handleBatchDelete() {
      if (this.selections.length == 0) {
        this.$message.warning("请选择要删除记录");
        return;
      }
      this.$confirm("确定要删除选中记录吗?", "删除提示：", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const sel_ids = {
            select_ids: this.selections,
          };
          const res = await delVoteDetails(sel_ids);

          this.getDetailList();

          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch((err) => {
          console.error(err);
          this.$message({
            type: "error",
            message: "删除失败!",
          });
        });
    },

    handleSelectionChange(val) {

      this.selections = val.map((item) => item.id);

    },

  },
};
</script>

<style scoped>
</style>